import math
from geopy.distance import geodesic


def calculate_distance(lat1, lon1, lat2, lon2):
    """
    计算两个经纬度点之间的距离。
    :param lat1: 第一个点的纬度（度数）
    :param lon1: 第一个点的经度（度数）
    :param lat2: 第二个点的纬度（度数）
    :param lon2: 第二个点的经度（度数）
    :return: 两个点之间的距离（米）
    """
    # 使用geodesic函数计算两个点之间的距离
    return geodesic((lat1, lon1), (lat2, lon2)).meters


def calculate_bearing(lat1, lon1, lat2, lon2):
    """
    Calculate the bearing (direction) from point 1 to point 2.
    Returns the bearing in degrees.
    """
    lat1 = math.radians(lat1)
    lon1 = math.radians(lon1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(lon2)
    delta_lon = lon2 - lon1
    x = math.cos(lat2) * math.sin(delta_lon)
    y = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(delta_lon)
    return (math.degrees(math.atan2(x, y)) + 360) % 360


def point_position_relative_to_bearing(point_lat, point_lon, angle, other_point_lat, other_point_lon):
    """
    Determine if the other point is in front of or behind the point based on the bearing.
    Returns 'front' if the other point is in front, 'behind' if it's behind.
    """
    point_bearing = calculate_bearing(point_lat, point_lon, other_point_lat, other_point_lon)
    if (angle - 90) <= point_bearing <= (angle + 90):
        # 前面
        return True
    else:
        # 后面
        return False
